const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const CompressionPlugin = require("compression-webpack-plugin");

module.exports = {
    mode: "production",
    optimization: {
        // 修改分包的名称开头 natural named
        chunkIds: 'deterministic',
        // 消除打包之后生成的LISENCE.txt文件
        minimizer: [
            new TerserPlugin({
                extractComments: false,
            }),
            new CssMinimizerPlugin()
        ],
        // 会把用到的第三方插件打包进一个公共的文件
        // initial处理同步  async处理异步
        splitChunks: {
            chunks: 'all',
            // 被引用一次就会被单独拆分
            minChunks: 1,
            // 对第三方插件的打包改名
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    filename: 'js/[id].vendor.js'
                }
            }
        },
        // 保存了打包入口文件对其他文件的引用关系数据
        runtimeChunk: true
    },
    plugins: [
        new CleanWebpackPlugin(),
        new CopyWebpackPlugin({
            patterns: [
                {
                    from: 'public',
                    globOptions: {
                        ignore: ['**/index.html']
                    }
                }
            ],
        }),
        new CompressionPlugin({
            test: /\.(css|js)$/,
        })
    ]
}